package com.example.jiaju.service.impl;

import com.example.jiaju.dataobject.User;
import com.example.jiaju.enums.ResultEnum;
import com.example.jiaju.exception.SMException;
import com.example.jiaju.repository.UserRepository;
import com.example.jiaju.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserRepository repository;

    @Override
    public User findByPhone(String phone) {
        return repository.findByPhone(phone);
    }

    @Override
    public User save(User user) {
        return repository.save(user);
    }

    @Override
    public User register(User user) {
        User user1=repository.findByPhone(user.getPhone());
        if(user1!=null){
            throw new SMException(ResultEnum.USER_IS_REGISTER);
        }
        return repository.save(user);
    }

    @Override
    public User updateName(String phone,String newName) {
        User user=repository.findByPhone(phone);
        if(user==null){
            throw new SMException(ResultEnum.USER_NOT_FOUND);
        }
        user.setName(newName);
        repository.save(user);
        return user;
    }


}